#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QString>
#include <QToolButton>
#include <QTextCursor>
#include <QVBoxLayout>

QString a; //用户输入数字
QString b; //用户输入符号
QString text;
bool Add =false;
bool Sub =false;
bool Mul = false;
bool Div = false;
bool Mod = false;



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("计算器"));
    this->setWindowIcon(QIcon(":/new/prefix1/calcator.png"));
    ui->plainTextEdit->setPlainText("");
    text = ui->plainTextEdit->toPlainText();
    QToolButton *numButtons[11];//11个按钮存放数组
    for(int i=0;i<11;i++)
    {
        QString numName = "num" +QString::number(i);
        numButtons[i] = MainWindow::findChild<QToolButton *>(numName);
        connect(numButtons[i],SIGNAL(clicked(bool)),this,SLOT(numOnclick()));
    }
    connect(ui->add,SIGNAL(clicked(bool)),this,SLOT(symbol()));
    connect(ui->reduce,SIGNAL(clicked(bool)),this,SLOT(symbol()));
    connect(ui->ride,SIGNAL(clicked(bool)),this,SLOT(symbol()));
    connect(ui->divide,SIGNAL(clicked(bool)),this,SLOT(symbol()));
    connect(ui->AC,SIGNAL(clicked(bool)),this,SLOT(symbol()));
    connect(ui->Delete,SIGNAL(clicked(bool)),this,SLOT(symbol()));
    connect(ui->right,SIGNAL(clicked(bool)),this,SLOT(symbol()));
    connect(ui->left,SIGNAL(clicked(bool)),this,SLOT(symbol()));
    connect(ui->equal,SIGNAL(clicked(bool)),this,SLOT(equalNum()));
    connect(ui->modular,SIGNAL(clicked(bool)),this,SLOT(symbol()));


}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::numOnclick()
{
    QToolButton *numName =(QToolButton*)sender();//sender()返回信号来源对象
    ui->plainTextEdit->textCursor().insertText(numName->text());
    text = ui->plainTextEdit->toPlainText();//字符串类型转换
    if(Add)
    {
        int i =text.indexOf("+");
        text = text.mid(i+1);
        b=text;

    }
    else if (Sub) {
        int i =text.indexOf("+");
        text = text.mid(i+1);
        b=text;
    }
    else if (Mul) {
        int i =text.indexOf("+");
        text = text.mid(i+1);
        b=text;
    }
    else if (Div) {
        int i =text.indexOf("+");
        text = text.mid(i+1);
        b=text;
    }
    else if (Mod) {
        int i =text.indexOf("%");
        text = text.mid(i+1);
        b=text;
    }
    else
        a=text;//只有数字情况
    qDebug()<<a<<b;
}
void MainWindow::symbol()
{
    QToolButton *pt = (QToolButton *)sender();
    QString p = pt->text();
    if(!(Add||Sub||Mul||Div||Mod))//有且输入一个运算符
    {
        if(p=="+")
        {
            Add = true;
            ui->plainTextEdit->textCursor().insertText("+");
        }
        if(p=="-")
        {
            Sub = true;
            ui->plainTextEdit->textCursor().insertText("-");
        }
        if(p=="×")
        {
            Mul = true;
            ui->plainTextEdit->textCursor().insertText("×");
        }
        if(p=="÷")
        {
            Div = true;
            ui->plainTextEdit->textCursor().insertText("÷");
        }
        if(p=="%")
        {
            Mod = true;
            ui->plainTextEdit->textCursor().insertText("%");
        }
    }
    if(p=="←")
    {
        text = ui->plainTextEdit->toPlainText();
        text.chop(1);
        Add=Sub=Mul=Div=Mod=false;
        matchFh();//识别输入
        ui->plainTextEdit->setPlainText(text);
        ui->plainTextEdit->moveCursor(QTextCursor::End);
    }
    if(p=="left")
        ui->plainTextEdit->moveCursor(QTextCursor::Left);
    if(p=="right")
        ui->plainTextEdit->moveCursor(QTextCursor::Right);
    if(p=="AC")
    {
        Add=Sub=Mul=Div=Mod=false;
        ui->plainTextEdit->setPlainText("");
    }
}
void MainWindow::matchFh(){//识别输入符号和分割
    //检测文本是否有各自输入符号
    if(text.contains("+",Qt::CaseSensitive))
    {
        QStringList t =text.split("+");
        a= t[0];
        b= t[1];
        Add =true;
    }
    else if (text.contains("-",Qt::CaseSensitive)) {
        QStringList t = text.split("-");
        a = t[0];
        b = t[1];
        Sub = true;
    }
    else if (text.contains("×",Qt::CaseSensitive)) {
        QStringList t = text.split("×");
        a = t[0];
        b = t[1];
        Mul = true;
    }
    else if (text.contains("÷",Qt::CaseSensitive)) {
        QStringList t = text.split("÷");
        a = t[0];
        b = t[1];
        Div = true;
    }
    else if (text.contains("%",Qt::CaseSensitive)) {
        QStringList t = text.split("%");
        a = t[0];
        b = t[1];
        Mod = true;
    }
    else a =text;
}
void MainWindow::equalNum()
{
    double x;
    double y;
    text = ui->plainTextEdit->toPlainText();
    matchFh();
    x= a.toDouble();
    y= b.toDouble();
    qDebug()<<"x:"<<x<<"y:"<<y;
    //代入运算
    if(Add)
    {
        ui->plainTextEdit->setPlainText(a=QString::number(x+y));
        Add = false;
    }
    if(Sub)
    {
        ui->plainTextEdit->setPlainText(a=QString::number(x-y));
        Sub = false;
    }
    if(Mul)
    {
        ui->plainTextEdit->setPlainText(a=QString::number(x*y));
        Mul = false;
    }
    if(Div)
    {
        ui->plainTextEdit->setPlainText(a=QString::number(x/y));
        Div = false;
    }
    if(Mod)
    {
        ui->plainTextEdit->setPlainText(a=QString::number((int)x%(int)y));
        Div = false;
    }
    ui->plainTextEdit->moveCursor(QTextCursor::End);
}
